var form = layui.form,id=getQueryVariable("id"),selectdata=[];
//表单初始值，修改时调用
var wjdata=JSON.parse(window.sessionStorage.getItem("wjMidifyData"));

queryPid();
//ajax得到所属上级目录 给select赋值
$("#pid").html("");
//var higherdirectory = [{name: 1, value: "text1"}, {name: 2, value: "text2"}, {name: 3, value: "text3"}];
for (var i = 0; i < selectdata.length; i++) {
    $("#pid").append("<option value=\"" + selectdata[i].id + "\">" + selectdata[i].name + "</option>");
}

if(id!=""){
    $("input[name='code']").attr("readOnly",true);
    form.val("editor-form",{
        "name":wjdata.name,
        "code":wjdata.code,
        "remark":wjdata.remark,
        "pid":wjdata.parentId
    })

}
form.render('select');

//监听提交
form.on('submit(form-element)', function(data){
    if(insert(data.field)){
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则刷新提交过来的界面 并关闭此页面
            parent.detab("views/jsp/BasicData/files.jsp");
            parent.intab("views/jsp/BasicData/files.jsp","文件")
            parent.detab("views/jsp/BasicData/tpl/newupfiles.jsp"+(id==''?'':'?id='+id));
        },1000)
    }else{
        layer.msg("保存失败", {icon: 6});
    }


    return false;
});
function queryPid() {
    $.ajax({
        type : 'get',
        //  url : 'resource/getTree',
        // data:{"ids":JSON.stringify(distributionid),"saleman":data.field.customersalesman},
        url:'wjcatalog/queryPid',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                selectdata=da.data;
                /* // datas=getdatas(da.data.children);

                  $("#higherdirectory").html("");
                // var higherdirectory = [{name: 1, value: "text1"}, {name: 2, value: "text2"}, {name: 3, value: "text3"}];
                 for (var i = 0; i < da.data.length; i++) {
                      $("#higherdirectory").append("<option value=\"" + da.data[i].id + "\">" + da.data[i].name + "</option>");
                  }*/
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
}
function  insert(data) {
    if(id !="") data.id=id;
    var tf=false;
    $.ajax({
        type : 'post',
        //  url : 'resource/getTree',
        data:JSON.stringify(data),
        url:'wjcatalog/insert',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf=da.data==0?false:true;

            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return tf;
}